﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace C9ShowNotesHelper.Utility
{
  static class LinkHelper
  {
    public enum LinkHTMLType
    {
      normal,
      asTableRow,
      asC9DLDR
    }
    
    static public string GenerateLink(TimeSpan time, string showURL, string episodeURL)
    {
      //<a href="http://channel9.msdn.com/Shows/This+Week+On+Channel+9/TWC9-Mar-16-2012#time=1m33s" target="_blank">[1:33]</a>
      
      string timetext = GenerateLinkText(time);

      string timeattr = GenerateTimeParameter(time);

      string link = string.Format("<a href=\"{0}/{1}{2} target=\"_blank\">", showURL, episodeURL, timeattr); // #time=1m33s"""" target="_blank">[1:33]</a>"

      string finallink = string.Format("{0}{1}</a>", link, timetext);

      return finallink;
    }
  
    static public string GenerateLink(TimeSpan time, string showURL, string episodeURL, string thumbnailpath, LinkHTMLType linktype)
    {
      //<a href="http://channel9.msdn.com/Shows/This+Week+On+Channel+9/TWC9-Mar-16-2012#time=1m33s" target="_blank"></a>
      
      string htmlfilename = "file://" + thumbnailpath.Replace("\\", "/");

      string timetext = GenerateLinkText(time);

      string timeattr = GenerateTimeParameter(time);

      string img = string.Format("<img src=\"{0}\" width=\"120\" height=\"60\" alt=\"{1}\" title=\"{1}\" >", htmlfilename, timetext);
         
      string link = string.Format("<a href=\"{0}/{1}{2} target=\"_blank\">", showURL, episodeURL, timeattr); // #time=1m33s"""" target="_blank">[1:33]</a>"
      
      string finallink = "";

      switch (linktype)
      {
        case LinkHTMLType.normal:
          finallink = string.Format("{0}{1}</a>", link, img);
          finallink += string.Format("{0}{1}</a>", link, timetext);
          break;
        case LinkHTMLType.asTableRow:
          string imglink = string.Format("<td valign=\"top\" width=\"200\">{0}{1}</a></td>", link, img);
          string textlink = string.Format("<td valign=\"top\" width=\"450\">{0}{1}</a></td>", link, timetext);
          finallink = string.Format("<tr>{0}{1}</tr>", imglink, textlink);
          break;
        
        case LinkHTMLType.asC9DLDR:
          string imglinkc9 = string.Format("<dt>{0}{1}</a></dt>", link, img);
          string textlinkc9 = string.Format("<dd>{0}{1}</a></dd>", link, timetext);
          finallink = string.Format("{0}{1}", imglinkc9, textlinkc9);
          break;
       }

      return finallink;
    }
    
    static public string GenerateLinkText(TimeSpan timestamp)
    {
      string timetext = "";

      if (timestamp.Minutes < 1)
        timetext = string.Format("[T0:{0:00}]", timestamp.Seconds);
      else
      {
        if (timestamp.Seconds > 0)
          timetext = string.Format("[T{0}:{1:00}]", timestamp.Minutes, timestamp.Seconds);
        else
          timetext = string.Format("[T{0}:00]", timestamp.Minutes);
      }

      return timetext;
    } 
        
    static private string GenerateTimeParameter(TimeSpan timestamp)
      {
        string timeattr = "";

       if (timestamp.Minutes < 1)
         timeattr = string.Format("#time={0}s\" ", timestamp.Seconds);
       else
       {
         if (timestamp.Seconds > 0)
           timeattr = string.Format("#time={0}m{1}s\" ", timestamp.Minutes, timestamp.Seconds);
         else
           timeattr = string.Format("#time={0}m\" ", timestamp.Minutes);
       }
       return timeattr;
     }
  }
}
